<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <!-- 主表单字段 -->
      <!-- <el-form-item label="仓库编号" prop="deptId">
        <el-input v-model="formData.deptId" placeholder="请输入仓库编号" disabled />
      </el-form-item> -->
      <el-form-item label="出库单号" prop="no">
        <el-input v-model="formData.no" placeholder="请输入出库单号" disabled />
      </el-form-item>
      <!-- <el-form-item label="核销报废类型" prop="type">
        <el-select v-model="formData.type" placeholder="请选择核销报废类型" disabled>
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.WRITEOFF_SCRAP_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="出库时间" prop="outTime">
        <el-date-picker
          v-model="formData.outTime"
          type="date"
          value-format="x"
          placeholder="选择出库时间"
          disabled
        />
      </el-form-item> -->
      <!-- <el-form-item label="合计数量" prop="totalCount">
        <el-input v-model="formData.totalCount" placeholder="请输入合计数量" disabled />
      </el-form-item> -->
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" disabled />
      </el-form-item>
      <!-- <el-form-item label="审核状态" prop="status">
        <el-select v-model="formData.status" placeholder="请选择状态" disabled>
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.WRITEOFF_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="图片" prop="fileUrl">
        <!-- <UploadImg v-model="formData.fileUrl" disabled /> -->
        <UploadImgs v-model="formData.fileUrl" disabled />
      </el-form-item>
    </el-form>

    <!-- 子表的表单 -->
    <el-tabs v-model="subTabsName">
      <el-tab-pane label="核销报废管理单项" name="checkOutItem">
        <CheckOutItemForm 
          ref="checkOutItemFormRef" 
          :out-id="formData.id" 
          :disabled="isSubFormDisabled"
        />
      </el-tab-pane>
    </el-tabs>

    <!-- 审核意见 -->
    <el-form-item label="审核意见" prop="comments" style="margin: 20px;" v-if="formData.status == 2">
        <el-input 
          v-model="formData.comments" 
          type="textarea" 
          :rows="4" 
          maxlength="500" 
          show-word-limit
        />
      </el-form-item>

    <!-- <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template> -->
  </Dialog>
</template>

<script setup lang="ts">
import { getStrDictOptions, getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { CheckOutApi, CheckOutVO } from '@/api/erp/checkout'
import CheckOutItemForm from './components/CheckOutItemFormDetail.vue'

/** ERP 核销报废管理单 表单 */
defineOptions({ name: 'CheckOutForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  deptId: undefined,
  no: undefined,
  type: undefined,
  outTime: undefined,
  totalCount: undefined,
  remark: undefined,
  status: undefined,
  fileUrl: [] as string[], // 修改为数组
  comments: undefined, // 审批意见
})
const formRules = reactive({
  deptId: [{ required: true, message: '仓库编号不能为空', trigger: 'blur' }],
  no: [{ required: true, message: '出库单号不能为空', trigger: 'blur' }],
  type: [{ required: true, message: '核销报废类型不能为空', trigger: 'change' }],
  // outTime: [{ required: true, message: '出库时间不能为空', trigger: 'blur' }],
  totalCount: [{ required: true, message: '合计数量不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 子表的表单 */
const subTabsName = ref('checkOutItem')
const checkOutItemFormRef = ref()
const isSubFormDisabled = ref(true) // 控制子表表单的禁用状态

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  // dialogTitle.value = t('action.' + type)
  dialogTitle.value = "详情"
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await CheckOutApi.getCheckOut(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    deptId: undefined,
    no: undefined,
    type: undefined,
    outTime: undefined,
    totalCount: undefined,
    remark: undefined,
    fileUrl: undefined,
  }
  formRef.value?.resetFields()
}
</script>